////////////////////////////////////////////////////////////////////////////////
//                                                                            //
// The MIT License (MIT)                                                      //
//                                                                            //
// Copyright (c) 2013 Charles Teague                                          //
//                                                                            //
// Permission is hereby granted, free of charge, to any person obtaining a    //
// copy of this software and associated documentation files (the "Software"), //
// to deal in the Software without restriction, including without limitation  //
// the rights to use, copy, modify, merge, publish, distribute, sublicense,   //
// and/or sell copies of the Software, and to permit persons to whom the      //
// Software is furnished to do so, subject to the following conditions:       //
//                                                                            //
// The above copyright notice and this permission notice shall be included in //
// all copies or substantial portions of the Software.                        //
//                                                                            //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR //
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   //
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    //
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER //
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    //
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        //
// DEALINGS IN THE SOFTWARE.                                                  //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using RPSDAL;
using RPSDAL.CollectionClasses;
using RPSDAL.FactoryClasses;
using RPSDAL.HelperClasses;
using RPSDAL.DaoClasses;
using RPSDAL.EntityClasses;
using RPSDAL.RelationClasses;

using SD.LLBLGen.Pro.ORMSupportClasses;


public partial class ContactHistory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack) 
        {
            int contactId = 0;
            if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Int32.TryParse(Global.DecryptString(Request.QueryString["id"].ToString()), out contactId)) 
            {
                grdContactHistory.PageSize = Parameters.PageSize;
                GetContactHistory(contactId);
            }
        }
    }

    private void GetContactHistory(int contactId)
    {
        DataTable dtFields = new DataTable();
        ResultsetFields fields = new ResultsetFields(7);

        fields.DefineField(ContactHistoryFieldIndex.Type, 0, "Type");
        fields.DefineField(ContactHistoryFieldIndex.LetterId, 1, "LetterId");
        fields.DefineField(ContactFieldIndex.Phone, 2, "Phone");
        fields.DefineField(ContactFieldIndex.MobilePhone, 3, "MobilePhone");
        fields.DefineField(BusinessRuleFieldIndex.EmailSubject, 4, "EmailSubject");
        fields.DefineField(ContactHistoryFieldIndex.Time, 5, "Time");
        fields.DefineField(BusinessRuleFieldIndex.Description, 6, "Description");
        

        RelationCollection relations = new RelationCollection();
        relations.Add(ContactHistoryEntity.Relations.CallEntityUsingCallId,JoinHint.Left);
        relations.Add(ContactHistoryEntity.Relations.ContactEntityUsingContactId, JoinHint.Inner);
        relations.Add(ContactHistoryEntity.Relations.BusinessRuleEntityUsingBusinessRuleId, JoinHint.Left);

        IPredicateExpression filter = new PredicateExpression();
        filter.Add(ContactHistoryFields.ContactId == contactId);

        TypedListDAO dao = new TypedListDAO();
        SortExpression sortExp = new SortExpression(new SortClause(fields["Time"], SortOperator.Descending));
        dao.GetMultiAsDataTable(fields, dtFields, 0, sortExp, filter, relations, false, null, null, 0, 0);

        grdContactHistory.DataSource = dtFields;
        grdContactHistory.DataBind();

        for (int i = 0; i < grdContactHistory.Rows.Count; i++) 
        {
            switch (Convert.ToInt32(((Label)grdContactHistory.Rows[i].Cells[1].FindControl("lblActionType")).Text)) 
            {
                case Parameters.CALL_TYPE:
                    ((Label)grdContactHistory.Rows[i].Cells[1].FindControl("lblActionType")).Text = "call";
                    break;
                case Parameters.LETTER_TYPE:
                    ((Label)grdContactHistory.Rows[i].Cells[1].FindControl("lblActionType")).Text = "letter";
                    break;
                case Parameters.MAIL_TYPE:
                    ((Label)grdContactHistory.Rows[i].Cells[1].FindControl("lblActionType")).Text = "mail";
                    break;
            }
        }
    }


    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("ContactList.aspx");
    }
    protected void grdContactHistory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdContactHistory.PageIndex = e.NewPageIndex;
        int contactId = 0;
        if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Int32.TryParse(Global.DecryptString(Request.QueryString["id"].ToString()), out contactId))
        {
            GetContactHistory(contactId);
        }
    }
}
